<?php
/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	
	+ Module  		: m_job Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_m_job.php
 	+ Author  		: 
 	+ Created on 24/Jul/2012 11:02:41
	
*/

//class of m_job
class C_m_job extends CI_Controller {

	//constructor
	function C_m_job(){
		parent::__construct();
		
		$this->load->model('m_m_job', '', TRUE);
		$this->load->model('m_m_div', '', TRUE);
		checkLogin();
	}
	
	//set index
	function index(){
		$this->load->view('main/v_m_job');
	}
	
	//for detail action
	//list detail handler action
	function  detail_m_jobdet_list(){
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$result=$this->m_m_job->detail_m_jobdet_list($master_id,$query,$start,$end);
		echo $result;
	}
	//end of handler
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->m_job_list();
				break;
			case "UPDATE":
				$this->m_job_update();
				break;
			case "CREATE":
				$this->m_job_create();
				break;
			case "DELETE":
				$this->m_job_delete();
				break;
			case "SEARCH":
				$this->m_job_search();
				break;
			case "PRINT":
				$this->m_job_print();
				break;
			case "EXCEL":
				$this->m_job_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function m_job_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_m_job->m_job_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function m_job_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$job_kode=trim(@$_POST["job_kode"]);
		$job_kode=htmlentities($job_kode,ENT_QUOTES);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=htmlentities($job_name,ENT_QUOTES);
		$job_active=trim(@$_POST["job_active"]);
		$job_div=trim(@$_POST["job_div"]);
		$job_div=htmlentities($job_div,ENT_QUOTES);
		//data detail
		$array_jobdet_id=@$_POST["jobdet_id"];
		$array_jobdet_id=json_decode(stripslashes($array_jobdet_id));
		$array_jobdet_name=@$_POST["jobdet_name"];
		$array_jobdet_name=json_decode(stripslashes($array_jobdet_name));
		
		$result=$this->m_m_job->m_job_create($job_kode ,$job_name ,$job_active ,$job_div, $array_jobdet_id, $array_jobdet_name);
		echo $result;
	}
	
	
	//function for update record
	function m_job_update(){
		//POST variable here
		$job_id=trim(@$_POST["job_id"]);
		$job_kode=trim(@$_POST["job_kode"]);
		$job_kode=str_replace("/(<\/?)(p)([^>]*>)", "",$job_kode);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "''",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "''",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "''",$job_created_by);
		$job_updated_date=@$_SESSION[SESSION_USERID];
		$job_updated_by=@$_SESSION[SESSION_USERID];
		$job_updated_count=@$_SESSION[SESSION_USERID];
		$m_div_div_id=trim(@$_POST["job_div"]);
		
		//data detail
		$array_jobdet_id=@$_POST["jobdet_id"];
		$array_jobdet_id=json_decode(stripslashes($array_jobdet_id));
		$array_jobdet_name=@$_POST["jobdet_name"];
		$array_jobdet_name=json_decode(stripslashes($array_jobdet_name));
		
		$result = $this->m_m_job->m_job_update($job_id,$job_kode,$job_name,$job_active,$job_updated_date,$job_updated_by,$job_updated_count,$m_div_div_id,$array_jobdet_id, $array_jobdet_name);
		echo $result;
	}
	
	//function for delete selected record
	function m_job_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_m_job->m_job_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function m_job_search(){
		//POST varibale here
		$job_id=trim(@$_POST["job_id"]);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "''",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "''",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "''",$job_created_by);
		$job_updated_date=trim(@$_POST["job_updated_date"]);
		$job_updated_by=trim(@$_POST["job_updated_by"]);
		$job_updated_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_by);
		$job_updated_by=str_replace("'", "''",$job_updated_by);
		$job_updated_count=trim(@$_POST["job_updated_count"]);
		$job_updated_count=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_count);
		$job_updated_count=str_replace("'", "''",$job_updated_count);
		$m_div_div_id=trim(@$_POST["m_div_div_id"]);
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_m_job->m_job_search($job_id ,$job_name ,$job_active ,$job_created_date ,$job_created_by ,$job_updated_date ,$job_updated_by ,$job_updated_count ,$m_div_div_id ,$start,$end);
		echo $result;
	}


	function m_job_print(){
  		//POST varibale here
		$job_id=trim(@$_POST["job_id"]);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "'",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "'",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "'",$job_created_by);
		$job_updated_date=trim(@$_POST["job_updated_date"]);
		$job_updated_by=trim(@$_POST["job_updated_by"]);
		$job_updated_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_by);
		$job_updated_by=str_replace("'", "'",$job_updated_by);
		$job_updated_count=trim(@$_POST["job_updated_count"]);
		$job_updated_count=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_count);
		$job_updated_count=str_replace("'", "'",$job_updated_count);
		$m_div_div_id=trim(@$_POST["m_div_div_id"]);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_m_job->m_job_print($job_id ,$job_name ,$job_active ,$job_created_date ,$job_created_by ,$job_updated_date ,$job_updated_by ,$job_updated_count ,$m_div_div_id ,$option,$filter);
		$print_view=$this->load->view("main/p_m_job.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_job_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function m_job_export_excel(){
		//POST varibale here
		$job_id=trim(@$_POST["job_id"]);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "\'",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "\'",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "\'",$job_created_by);
		$job_updated_date=trim(@$_POST["job_updated_date"]);
		$job_updated_by=trim(@$_POST["job_updated_by"]);
		$job_updated_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_by);
		$job_updated_by=str_replace("'", "\'",$job_updated_by);
		$job_updated_count=trim(@$_POST["job_updated_count"]);
		$job_updated_count=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_count);
		$job_updated_count=str_replace("'", "\'",$job_updated_count);
		$m_div_div_id=trim(@$_POST["m_div_div_id"]);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		$data["type"]="excel";
		$data["data_print"] = $this->m_m_job->m_job_export_excel($job_id ,$job_name ,$job_active ,$job_created_date ,$job_created_by ,$job_updated_date ,$job_updated_by ,$job_updated_count ,$m_div_div_id ,$option,$filter);
		$print_view=$this->load->view("main/p_m_job.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_job_printlist.xls","w+");
		fwrite($print_file, $print_view);
		echo '1';  
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
	
	
}
?>